﻿using System;
using UnityEngine;

// Token: 0x02000120 RID: 288
[RequireComponent(typeof(Camera))]
[ExecuteInEditMode]
public class RendAndDepthTexture : MonoBehaviour
{
	// Token: 0x170000BC RID: 188
	// (get) Token: 0x0600069B RID: 1691 RVA: 0x000270C4 File Offset: 0x000252C4
	public RenderTexture ColorTex
	{
		get
		{
			return this.colorTex;
		}
	}

	// Token: 0x170000BD RID: 189
	// (get) Token: 0x0600069C RID: 1692 RVA: 0x000270CC File Offset: 0x000252CC
	public RenderTexture DepthTex
	{
		get
		{
			return this.depthTex;
		}
	}

	// Token: 0x0600069D RID: 1693 RVA: 0x000270D4 File Offset: 0x000252D4
	private void OnEnable()
	{
		if (!Application.isPlaying)
		{
			return;
		}
		this.CreateTexture();
	}

	// Token: 0x0600069E RID: 1694 RVA: 0x000270E8 File Offset: 0x000252E8
	private void OnDisable()
	{
		if (!Application.isPlaying)
		{
			return;
		}
		this.ReleaseTexture();
	}

	// Token: 0x0600069F RID: 1695 RVA: 0x000270FC File Offset: 0x000252FC
	private void OnPreRender()
	{
		if (!Application.isPlaying)
		{
			return;
		}
		if (this.colorTex.width != Screen.width || this.colorTex.height != Screen.height)
		{
			this.RemakeTexture();
		}
	}

	// Token: 0x060006A0 RID: 1696 RVA: 0x0002713C File Offset: 0x0002533C
	private void OnPostRender()
	{
		if (!Application.isPlaying)
		{
			return;
		}
		Graphics.SetRenderTarget(null);
		if (this.postMat == null)
		{
			return;
		}
		Graphics.Blit(this.colorTex, this.postMat);
	}

	// Token: 0x060006A1 RID: 1697 RVA: 0x00027174 File Offset: 0x00025374
	private void CreateTexture()
	{
		this.camera = base.GetComponent<Camera>();
		this.camera.depthTextureMode = DepthTextureMode.Depth;
		RenderTextureFormat format = (!this.camera.allowHDR) ? RenderTextureFormat.Default : RenderTextureFormat.DefaultHDR;
		if (this.colorTex == null)
		{
			this.colorTex = new RenderTexture(Screen.width, Screen.height, 0, format);
		}
		else
		{
			this.colorTex.Release();
			this.colorTex.width = Screen.width;
			this.colorTex.height = Screen.height;
			this.colorTex.depth = 0;
			this.colorTex.format = format;
			this.colorTex.Create();
		}
		this.colorTex.Create();
		if (this.depthTex == null)
		{
			this.depthTex = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.Depth);
		}
		else
		{
			this.depthTex.Release();
			this.depthTex.width = Screen.width;
			this.depthTex.height = Screen.height;
			this.depthTex.depth = 24;
			this.depthTex.format = RenderTextureFormat.Depth;
			this.depthTex.Create();
		}
		this.depthTex.Create();
		this.camera.SetTargetBuffers(this.colorTex.colorBuffer, this.depthTex.depthBuffer);
		this.createdTex = true;
	}

	// Token: 0x060006A2 RID: 1698 RVA: 0x000272EC File Offset: 0x000254EC
	private void ReleaseTexture()
	{
		if (!this.createdTex)
		{
			return;
		}
		Graphics.SetRenderTarget(null);
		this.camera.targetTexture = null;
		this.camera.SetTargetBuffers(Graphics.activeColorBuffer, Graphics.activeDepthBuffer);
		if (this.colorTex != null)
		{
			this.colorTex.Release();
			this.colorTex = null;
		}
		if (this.depthTex != null)
		{
			this.depthTex.Release();
			this.depthTex = null;
		}
	}

	// Token: 0x060006A3 RID: 1699 RVA: 0x00027374 File Offset: 0x00025574
	private void RemakeTexture()
	{
		this.camera.targetTexture = null;
		this.colorTex.Release();
		this.colorTex.width = Screen.width;
		this.colorTex.height = Screen.height;
		this.colorTex.Create();
		this.depthTex.Release();
		this.depthTex.width = Screen.width;
		this.depthTex.height = Screen.height;
		this.depthTex.Create();
		this.camera.SetTargetBuffers(this.colorTex.colorBuffer, this.depthTex.depthBuffer);
	}

	// Token: 0x0400065B RID: 1627
	private Camera camera;

	// Token: 0x0400065C RID: 1628
	private RenderTexture colorTex;

	// Token: 0x0400065D RID: 1629
	private RenderTexture depthTex;

	// Token: 0x0400065E RID: 1630
	[SerializeField]
	private Material postMat;

	// Token: 0x0400065F RID: 1631
	private bool createdTex;
}
